package co.yixiang.hw.service.impl;

import co.yixiang.hw.domain.YxUserAddress;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.hw.repository.YxUserAddressRepository;
import co.yixiang.hw.service.YxUserAddressService;
import co.yixiang.hw.service.dto.YxUserAddressDto;
import co.yixiang.hw.service.dto.YxUserAddressQueryCriteria;
import co.yixiang.hw.service.mapper.YxUserAddressMapper;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import javax.annotation.Resource;


/**
* @author Walert
* @date 2020-02-18
*/
@Service
//@CacheConfig(cacheNames = "yxUserAddress")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxUserAddressServiceImpl implements YxUserAddressService {

    @Resource
    private YxUserAddressRepository yxUserAddressRepository;
    @Resource
    private YxUserAddressMapper yxUserAddressMapper;


    @Override
    //@Cacheable
    public Map<String,Object> queryAll(YxUserAddressQueryCriteria criteria, Pageable pageable){
        Page<YxUserAddress> page = yxUserAddressRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(yxUserAddressMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<YxUserAddressDto> queryAll(YxUserAddressQueryCriteria criteria){
        return yxUserAddressMapper.toDto(yxUserAddressRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxUserAddressDto findById(Integer id) {
        YxUserAddress yxUserAddress = yxUserAddressRepository.findById(id).orElseGet(YxUserAddress::new);
        ValidationUtil.isNull(yxUserAddress.getId(),"YxUserAddress","id",id);
        return yxUserAddressMapper.toDto(yxUserAddress);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public YxUserAddressDto create(YxUserAddress resources) {
        return yxUserAddressMapper.toDto(yxUserAddressRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(YxUserAddress resources) {
        YxUserAddress yxUserAddress = yxUserAddressRepository.findById(resources.getId()).orElseGet(YxUserAddress::new);
        ValidationUtil.isNull( yxUserAddress.getId(),"YxUserAddress","id",resources.getId());
        yxUserAddress.copy(resources);
        yxUserAddressRepository.save(yxUserAddress);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(JSONArray ids) {
        for (int i = 0; i < ids.size(); i++) {
            yxUserAddressRepository.deleteById(ids.getInteger(i));
        }
    }

    /**
     * 根据用户ID设置用户收货地址非默认状态
     *
     * @param uid
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void settingNoDefaultByUid(Integer uid) {
        yxUserAddressRepository.updateDefaultStatus(uid);
    }

    public List<YxUserAddress> findAllByUid(Integer uid){
        return yxUserAddressRepository.findAllByUidAndIsDelOrderByIsDefaultDesc(uid,0);
    }
}